<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Fenxiao_Rank extends Stourweb_Controller
{
    public function before()
    {
        parent::before();
    }

    //等级列表
    public function action_list()
    {

        $action=$this->params['action'];
        if(empty($action))
        {
            $this->display('admin/fenxiao/rank/list');
        }
        else if($action=='read')
        {
           $list=ORM::factory('fenxiao_rank')->get_all();
            foreach($list as &$row)
            {
                $row['membernum']= ORM::factory('fenxiao')->where('fxrankid','=',$row['id'])->count_all();
                $jifen_info = DB::select()->from('jifen')->where('id','=',$row['jifen_id'])->execute()->current();
                $row['jifen'] = $jifen_info['value'];

            }



            $result['total'] = count($list);
            $result['lists'] = $list;
            $result['success'] = true;
            echo json_encode($result);
        }
        else if($action=='save') //保存修改
        {
            $rawdata=file_get_contents('php://input');
            $field=Arr::get($_GET,'field');
            $data=json_decode($rawdata);
            $id=$data->id;
            if($field)
            {
                $model=ORM::factory('fenxiao_rank',$id);
                if($model->id)
                {
                    $model->$field=$data->$field;
                    $model->save();
                    if($model->saved())
                        echo 'ok';
                    else
                        echo 'no';
                }
            }

        }

        else if($action=='delete')//属性删除
        {
            $rawdata=file_get_contents('php://input');
            $data=json_decode($rawdata);
            $id=$data->id;
            if(!is_numeric($id))
            {
                echo json_encode(array('success'=>false));
                exit;
            }
            $model=ORM::factory('fenxiao_rank',$id);
            $model->delete();

        }
        else if($action=='update')//更新操作
        {
            $id=Arr::get($_POST,'id');
            $field=Arr::get($_POST,'field');
            $val=Arr::get($_POST,'val');
            $model=ORM::factory('fenxiao_rank',$id);
            if($model->id)
            {
                $model->$field=$val;
                $model->save();
                if($model->saved())
                    echo 'ok';
                else
                    echo 'no';
            }
        }


    }
    //添加等级
    public function action_add()
    {

        $this->display('admin/fenxiao/rank/edit');
    }
    //编辑
    public function action_edit()
    {
        $id = $this->params['id'];
        $info = DB::select()->from('fenxiao_rank')->where('id','=',$id)->execute()->current();
        $jifen = DB::select()->from('jifen')->where('id','=',$info['jifen_id'])->execute()->current();

        $this->assign('info',$info);
        $this->assign('jifen',$jifen);
        $this->display('admin/fenxiao/rank/edit');
    }

    //保存
    public function action_ajax_save()
    {
        $id = $_POST['id'];
        $title = $_POST['title'];
        $min_order_num = intval($_POST['min_order_num']);
        $max_order_num = intval($_POST['max_order_num']);
        $jifen_id = $_POST['jifen_id'];
        $model = ORM::factory('fenxiao_rank',$id);
        $model->title = $title;
        $model->min_order_num = $min_order_num;
        $model->max_order_num = $max_order_num;
        $model->jifen_id = $jifen_id;
        $model->save();
        if($model->saved())
        {
            Model_Fenxiao::update_member_rank();
            echo json_encode(array('status'=>true,'msg'=>'保存成功','id'=>$model->id));
        }
        else
        {
            echo json_encode(array('status'=>false,'msg'=>'保存失败'));
        }

    }

    //对话框
    public function action_dialog_jifen()
    {
        $this->display('admin/fenxiao/rank/dialog_jifen');
    }

    //获取积分列表
    public function action_ajax_get_jifens()
    {
        $keyword = $_POST['keyword'];
        $page = intval($_POST['page']);
        $page = $page<1?1:$page;
        $pagesize = 8;
        $offset = $pagesize*($page-1);

        $w = " where label like 'sys_fenxiao_%' and isopen=1 ";
        if(!empty($keyword))
        {
            $w.=" and title like '%{$keyword}%' ";
        }

        $sql = "select * from sline_jifen {$w} order by id limit {$offset},{$pagesize}";
        $sql_num = " select count(*) as num from sline_jifen {$w}";

        $list = DB::query(Database::SELECT,$sql)->execute()->as_array();
        $num = DB::query(Database::SELECT,$sql_num)->execute()->get('num');
        echo json_encode(array('list'=>$list,'pagesize'=>$pagesize,'page'=>$page,'total'=>$num));
    }

}